"""pandas的100个练习题"""
import numpy as np
import pandas as pd


def tail_version():
    """
    查看pandas的版本和信息
    :return:
    """
    print("当前pandas的版本为：", pd.__version__)
    print("当前pandas的信息为：", pd.show_versions())


def create_series():
    """
    List、NumpyArray、Dict转化成series
    :return:None
    """
    list1 = list("abcdef")
    series1 = pd.Series(list1, index=list("ABCDEF"), name="list转化成series", copy=False)
    print("list1:\n", list1)
    print("series1:\n", series1)
    my_arr = np.arange(6)
    series_arr = pd.Series(my_arr)
    print("my_arr:\n", my_arr)
    print("series_arr:\n", series_arr)
    my_dict = dict(zip(list1, my_arr))
    series_dict = pd.Series(my_dict, index=list("abcdeh"))
    print("my_dict:\n", my_dict)
    print("series_dict:\n", series_dict)


def serice_index_to_df_column():
    """
    将series的index转换成DataFrame的column
    :return:None
    """
    serices = pd.Series(np.random.randn(8), index=list("abcdefgh"))
    series_index = serices.index  # 获取series的index
    # 创建DataFrame，指定columns
    df = pd.DataFrame(np.arange(24).reshape(3, 8), index=list("123"), columns=series_index)
    print(df)
    df2 = serices.to_frame().reset_index()  # 将series直接转换成DataFrame
    print("*" * 100)
    print(serices)
    print("*" * 100)
    print(df2)
    print(df2.index)
    print(df2.columns)


def series_to_df():
    """
    多个series合并成一个DataFrame
    :return:None
    """
    serices1 = pd.Series(np.random.randn(3), index=list("abc"))
    serices2 = pd.Series(np.random.randn(3), index=list("123"))
    df = pd.DataFrame([serices1, serices2])

    df2 = pd.DataFrame({"serices": serices1, "serices2": serices2})
    print("*" * 100)
    print(serices1)
    print("*" * 100)
    print(serices2)
    print("*" * 100)
    print([serices1, serices2])
    print("*" * 100)
    print(df)
    print("*" * 100)
    print(df2)


def series_to_df_as_idx():
    """
    根据index，将多个series合并成DataFrame
    :return:None
    """
    serices1 = pd.Series(np.random.randn(3), index=list("abc"))
    serices2 = pd.Series(np.random.randn(3), index=list("123"))
    data = pd.concat([serices1, serices2], axis=1)
    print("serices1:\n", serices1)
    print("*" * 100)
    print("serices2:\n", serices2)
    print("*" * 100)
    print("data:\n", data)


def find_series():
    """
    找到seriesA中不在seriesB中的数据
    :return:
    """
    serices1 = pd.Series(np.random.randn(3), index=list("abc"))
    serices2 = serices1.copy()
    rs = serices1.isin(serices2)
    print("serices1:\n", serices1)
    print("*" * 100)
    print("serices2:\n", serices2)
    print("*" * 100)
    print(rs)
    print(serices1[rs])


def series_join():
    """
    两个series的并集
    :return:
    """
    serices1 = pd.Series(np.random.randn(3), index=list("123"))
    serices2 = pd.Series(np.random.randn(3), index=list("123"))
    serices3 = np.union1d(serices1, serices2)
    print("serices1:\n", serices1)
    print("*" * 100)
    print("serices2:\n", serices2)
    print("*" * 100)
    print(serices3)


def series_percentile():
    """
    如何获得series的最小值，第25百分位数，中位数，第75位和最大值？
    :return:
    """
    data = pd.Series(np.random.normal(10, 5, 25))
    print(data)
    np.percentile(data, [25, 50, 75, 100])


def series_values_count():
    """
    获得系列中唯一项目的频率计数
    :return:
    """
    index = pd.Index([3, 1, 2, 3, 4, np.nan])
    print(index.value_counts())
    data = pd.Series(np.random.randint(5, 10, 25))
    print("*" * 100)
    print("data:\n", data)

    print("value_count:\n",data.value_counts(normalize=False)) #normalize=False,返回具体统计个数
    print("value_count:\n",data.value_counts(normalize=True)) #normalize=True,返回概率值
    print("value_count_index:\n",data.value_counts().index[0:2])
    print("value_count_head:\n",data.value_counts().head(2))


if __name__ == "__main__":
    # tail_version()
    # create_series()
    # serice_index_to_df_column()
    # series_to_df()
    # series_to_df_as_idx()
    # find_series()
    # series_join()
    # series_percentile()
    series_values_count()
